/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cafe.Controller;

import cafe.DataObject.Catalog;
import cafe.DataObject.Food;
import cafe.Model.CatalogModel;
import cafe.Model.FoodModel;
import hapax.TemplateDataDictionary;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.log.Log;

/**
 *
 * @author TieuHan
 */
public class FoodController extends ControllerInterface {

    private static final org.eclipse.jetty.util.log.Logger LOG = Log.getLogger(FoodController.class);
    CatalogModel catalogModel = CatalogModel.getInstance();
    FoodModel foodModel = FoodModel.getInstance();

    @Override
    public String doAction(String action, HttpServletRequest request, HttpServletResponse response, String context) {
        switch (action) {
            case "additem":
                LOG.info("Add Food");
                return addItem(request, response, context);
            case "delete":
                LOG.info("Delete Food");    
                return delete(request, response, context);
            case "getitem":
                break;
            case "getpageAdditem":
                
                return listItem(request, response, context, "/View/themmonan.xtm");
            /*
             Writer: ANNN@vng.com
             Date: 26/06/2014
             */
            case "edit":
                LOG.info("Edit Food");
                return edit(request, response, context);
            case "update":
                LOG.info("Update Food");
                return edit(request, response, context);
            /*
             End
             */
            default:
                LOG.info("Default Food Action: Index");
                return index(request, response, context, "/View/monan.xtm");
        }
        return "";

    }

    /*
     Writer: ANNN@vng.com
     Date: 26/6/2014
     */
    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, String context) {
        if (request.getParameter("do").equals("edit")) {
            return RenderPage(request, response, context, "/View/Ajax/ajax_suamonan.xtm");
        } else {
            if (request.getParameter("do").equals("update")) {
                foodModel.edit(request.getParameter("id"),
                        new Food(
                                request.getParameter("name"),
                                request.getParameter("catalog"),
                                Long.parseLong(request.getParameter("price"))));

                return RenderPage(request, response, context, "/View/Ajax/ajax_monan.xtm");
            }
        }
        return null;
    }

    private String RenderPage(HttpServletRequest request, HttpServletResponse response, String context, String file) {
        Food food = foodModel.getByID(request.getParameter("id"));
        TemplateDataDictionary cmDic = templateCollection.getTemplateDataDictionary();
        List<Catalog> catalogList = (List<Catalog>) catalogModel.getAll();

        cmDic.setVariable("Food.id", food.getId());
        cmDic.setVariable("Food.name", food.getName());
        cmDic.setVariable("Food.price", String.valueOf(food.getPrice()));

        for (Catalog catalog : catalogList) {
            if (food.getCategoricalId().equals(catalog.getId())) {
                cmDic.setVariable("Food.categoryName", catalog.getName());
                break;
            } else {
                cmDic.setVariable("Food.categoryName", "NULL");
            }
        }

//        LOG.info(String.valueOf(catalogList.size()));
        for (Catalog catalog : catalogList) {
            cmDic = templateCollection.getTemplateDataDictionary().addSection("Catalog");

//            LOG.info(catalog.getId());
//            LOG.info(catalog.getName());
            cmDic.setVariable("Catalog.id", catalog.getId());
            cmDic.setVariable("Catalog.name", catalog.getName());
        }

        return index(request, response, context, file);
    }

    /*
     End
     */
    @Override
    public String listItem(HttpServletRequest request, HttpServletResponse response, String context, String URL) {

        List<Catalog> catalogList = (List<Catalog>) catalogModel.getAll();

//        LOG.info(String.valueOf(catalogList.size()));
        for (Catalog catalog : catalogList) {
            TemplateDataDictionary cmDic = templateCollection.getTemplateDataDictionary().addSection("Catalog");

//            LOG.info(catalog.getId());
//            LOG.info(catalog.getName());
            cmDic.setVariable("Catalog.id", catalog.getId());
            cmDic.setVariable("Catalog.name", catalog.getName());
        }

        return super.listItem(request, response, context, URL); //To change body of generated methods, choose Tools | Templates.
    }

  

    // Load trang monan.xtm
    @Override
    public String index(HttpServletRequest request, HttpServletResponse response, String context, String URL) {

        List<Catalog> catalogList = (List<Catalog>) catalogModel.getAll();
//        LOG.info(String.valueOf(catalogList.size()));
        List<Food> foodList = (List<Food>) foodModel.getAll();
//        LOG.info(String.valueOf(foodList.size()));

        for (Food food : foodList) {
            TemplateDataDictionary cmDic = templateCollection.getTemplateDataDictionary().addSection("Food");
            cmDic.setVariable("Food.id", food.getId());
            cmDic.setVariable("Food.name", food.getName());

            for (Catalog catalog : catalogList) {
                if (food.getCategoricalId().equals(catalog.getId())) {
                    cmDic.setVariable("Food.categoryName", catalog.getName());
                    break;
                } else {
                    cmDic.setVariable("Food.categoryName", "NULL");
                }
            }

            cmDic.setVariable("Food.price", String.valueOf(food.getPrice()));
        }
        //set variable to section

        return super.index(request, response, context, URL); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String addItem(HttpServletRequest request, HttpServletResponse response, String context) {

//        LOG.info(String.valueOf(foodModel.getAll().size() + 1));
//        LOG.info(request.getParameter("name"));
//        LOG.info(request.getParameter("catalog"));
//        LOG.info(request.getParameter("price"));
        foodModel.insert(new Food(request.getParameter("name"),
                request.getParameter("catalog"),
                Long.parseLong(request.getParameter("price"))));

        return index(request, response, context, "View/monan.xtm");
    }

    @Override
    public String delete(HttpServletRequest request, HttpServletResponse response, String context) {

//        LOG.info(request.getParameter("id"));
        foodModel.delete(request.getParameter("id"));

        return index(request, response, context, "View/monan.xtm");
    }

}
